package com.carey.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carey.yygh.cmn.listener.readlinstener;
import com.carey.yygh.cmn.mapper.DictMapper;
import com.carey.yygh.cmn.service.DictService;
import com.carey.yygh.model.cmn.Dict;
import com.carey.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author carey-
* @description 针对表【dict(组织架构表)】的数据库操作Service实现
* @createDate 2022-05-11 20:05:47
*/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict>
    implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Override
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public List<Dict> findChildDate(Long id) {
        List<Dict> dictList = dictMapper.selectAllByParentId(id);
        //设置是否有子节点
        dictList.forEach(dict -> {
            Long dictId = dict.getId();
            boolean flag = isChild(dictId);
            dict.setHasChildren(flag);
        });
        return dictList;
    }

    //判断ID是否有子节点
    @Override
    public boolean isChild(Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",id);
        Integer count = dictMapper.selectCount(queryWrapper);
        return count>0;
    }

    @Override
    public void exprotData(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");//传的是excel表格
        response.setCharacterEncoding("utf-8");
        String fileName = "dict";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        List<Dict> list = dictMapper.selectList(null);
        //list中的象转换 Dict -> DictEeVo
        List<DictEeVo> eeVoList = new ArrayList<>();
        for (Dict dict : list) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            eeVoList.add(dictEeVo);
        }
        //输出excel
        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class)
                    .sheet("dict")
                    .doWrite(eeVoList);
        } catch (IOException e) {
            System.out.println("异常");
        }
    }

    @Override
    @CacheEvict(value = "dict", allEntries=true)
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new readlinstener(dictMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getName(String dictcode, String value) {
        //判断dictcode是否为空
        if (StringUtils.isEmpty(dictcode)){
            //为空 直接根据value查
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("value",value);
            Dict dict = dictMapper.selectOne(queryWrapper);
            return dict.getName();
        }else {
            //不为空 先通过dictcode查询得到父id
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_code",dictcode);
            Dict dict = dictMapper.selectOne(queryWrapper);
            Long id = dict.getId();
            //通过父id和value查询name
            QueryWrapper<Dict> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("parent_id",id);
            queryWrapper2.eq("value",value);
            Dict dict2 = dictMapper.selectOne(queryWrapper2);
            return dict2.getName();
        }

    }

    @Override
    public List<Dict> finByDictCode(String dictCode) {
        QueryWrapper<Dict> wapper = new QueryWrapper<>();
        wapper.eq("dict_code",dictCode);
        Dict dict = dictMapper.selectOne(wapper);
        if (null==dict){
            return null;
        }
        List<Dict> list = this.findChildDate(dict.getId());
        return list;
    }
}




